package com.ruoyi.common.utils.lpr;

import com.ruoyi.common.result.ResultInfo;
import com.ruoyi.common.utils.exception.CustomException;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

/**
 * @author 党怡承
 * @description 车牌结果集工具类
 * @since 2021-08-09
 */
@Component
public class LicensePlate {

    public static String licensePlate(String imgPath) {
        try {
            String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
            byte[] image = FileUtil.readFileByBytes(imgPath);
            String imageUrl = Base64Util.encode(image);
            String imgParam = URLEncoder.encode(imageUrl, "UTF-8");
            String param = "image=" + imgParam;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = DetectionToken.getToken();
            String result = HttpUtil.post(url, accessToken, param);
            return result;
        } catch (Exception e) {
            throw new CustomException(20001,
                    ResultInfo.PLATENUMBER_NOT_RESULT_LIST_SELECTELSEIMAGE);
        }
    }
}
